<?php
class ApiCenter_ProductApi_Model_Api2_Product_Attribute_Rest_Admin_V1 extends  ApiCenter_ProductApi_Model_Api2_Product_Attribute_Rest{
    
    const CN = "3";
    
    private   $step_main=array(
        "General",
        "Basic Product Information",
        "Prices",
    );

    private  $step1Map = array(
        "name",
        "common_product_name",
        "description",
        "short_description"
    );
    private  $step2Map = array(
        "common_brand",
        "price",
        "special_price",
        "special_from_date",
        "special_to_date",
        "common_other_platform_link",
        "common_other_platform_price",
        "common_is_charged",
        "common_msds",
        "common_package_length",
        "common_package_width",
        "common_package_height",
        "common_shipping_type",
        "common_warehouse_code",
    );
    
    private  $imageMap = array(
        "image"=>"is_main",
        "small_image"=>"is_smallimage",
        "thumbnail"=>"is_thumbnail",
    );
    private   $step_3 =array(
        "Additional Product Information",
    );
    
   private $filterArr = array(
       "common_reasons",//拒绝原因'字段，新增时不可见
       "pulscen",
        "giftcard_amounts",
       "allow_open_amount",
       "common_weight",
       "open_amount_min",
       "open_amount_max",
       "group_price",
       "tier_price",
       "common_supply_price",
       "msrp_enabled",
       "msrp_display_actual_price_type",
       "common_supplier_special_price",
         "attr_giftcard_amounts",
        "fb_product",
       "news_from_date",
       "news_to_date",
       "url_key",
       "country_of_manufacture",
       "is_returnable",
       "em_featured",
       "em_deal",
       "em_hot",
       "yml",
        "sku",
       "vendor_id",
        "visibility",
       "status",
        "approval",
       "cost",
       "yandex_market_product_category",
       "tax_class_id",
       "tiers_table",
       "msrp",
       "common_other_shipping_type",
       "common_publish_platform",
       "common_shipping_method",
       "common_dimensional_weight",
       "inventory_manage_stock",
       "inventory_min_sale_qty",
       "inventory_max_sale_qty",
       "inventory_is_qty_decimal",
       "inventory_notify_stock_qty",
       "inventory_enable_qty_increments",
       "inventory_backorders",
       "simple_product_visibility",
   );
   public $childAttr =array(
         "weight",
         "vendor_sku"
   );
  public  $stock_qty=array(
   "attribute_code" => "qty",
   "frontend_label" => "库存",
    "is_required"  =>"1",
    "frontend_input"=>"text"
  );
  
   private $readonly = array(
       "price",
       "special_price",
       "cost",
       "common_reasons",
       
   );
   public $warehouse_code =array(
       "attribute_code" => "common_warehouse_code",
       "frontend_label" => "默认仓库",
       "is_required"  =>"1",
       "frontend_input"=>"select"
   );
   private  $hidden = array(
       
   );
    protected function _retrieve(){
        $vendorId  =$this->getRequest()->getParam('vendorid');
        try{
            
        $setId = $this->getRequest()->getParam('setid');
        $this->_checkProductAttributeSet($setId);
        /* @var $helper ApiCenter_ProductApi_Helper_Data */
        $helper = Mage::helper("productapi");
        
        /* @var $helper ApiCenter_VendorsApi_Helper_Data */
        $ves_helper = Mage::helper("vendorsapi");
         $collection = Mage::getResourceModel('catalog/product_attribute_collection')
        ->addStoreLabel(self::CN)
        ->setAttributeSetFilter($setId)
        ->addVisibleFilter()
        ->load();
        
        $attr = $collection->getData();
        
        /* @var $helper ApiCenter_ProductApi_Helper_Data */
        $helper = Mage::helper("productapi");
        $product_attr=array(
            "step_1"=>array(
					"name" => "产品描述（步骤一）：",
					"data" => array(),
					),
            "step_2"=>array(
					"name" => "产品信息（步骤二）：",
					"data" => array(),
					),
            "step_3"=>array(
					"name" => "产品规格（步骤三）",
					"data" => array(),
					),
            );
			$other_attr=array(
					"super_attr"=>array(
					"name" => "超级属性",
					"data" => array(),
					),
      
				   "others"=>array(
						"name" => "其他",
						"data" => array(),
						),
			);
				$main_attr = array(
				    "data" => array(),
			);
        foreach ($attr as $i=>$val){
            $group_name = $helper->getAttributeGroupAction($val['attribute_group_id']);
        
            if($val['frontend_input']== "select"||$val['frontend_input']== "multiselect"){
                $val['select_options']= $helper->getAttributesOption($val['attribute_id']);
            }
            if(in_array($val['attribute_code'], $this->readonly)) $val["read_only"]=1;
            if (in_array($val['attribute_code'], $this->filterArr)) continue;
            
			if(in_array($val['attribute_code'],$this->childAttr)){
				$other_attr["others"]['data'][]= $val;
				if($val['attribute_code']!=="weight"){
				    $main_attr['data'][]= $val;
				}
				 continue;
			}
            if($val['is_global'] == '1' && $val['frontend_input'] == 'select' && $val['is_configurable']){
                array_shift($val['select_options']);
                if($this->matchImageLabel($val['attribute_id'])){
                    $val['image_label']=1;
                }
                $other_attr["super_attr"]['data'][]= $val;
                continue;
            }
           
            
            if(in_array($group_name,$this->step_main)){
                $step_main[]=$val;
            }
            
            if(in_array($group_name, $this->step_3)){
                $product_attr["step_3"]['data'][]= $val;
            }
        }
            $other_attr["others"]['data'][]= $this->stock_qty;
          
            //步骤一
            foreach ($this->step1Map as $attribute){
                foreach ($step_main as $_attribute){
                    if($attribute== $_attribute['attribute_code']){
                        $product_attr["step_1"]['data'][]=$_attribute;
                    }
                }
            }
            //步骤二
            foreach ($this->step2Map as $attribute){
                foreach ($step_main as $_attribute){
                    if($attribute== $_attribute['attribute_code']){
                        if($_attribute['attribute_code']=="common_warehouse_code"){
                            $this->warehouse_code['product_value'] =  $val["product_value"];
                            $this->warehouse_code['select_options']=$ves_helper->loadWareHouseOption($vendorId);
                            $_attribute= $this->warehouse_code;
                        }
                        $product_attr["step_2"]['data'][]=$_attribute;
                    }
                }
            }
            
         $filter_attr['step']=$product_attr;
		 $filter_attr['super']=$other_attr;
		 $filter_attr['main'] = $main_attr;
            $res['status'] =1;
            $res['info'] =$filter_attr;
        } catch (Exception $e) {
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }
        return $res;
    }
   
}